package com.yc.web.servlets.admin;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yc.bean.tb_order;
import com.yc.bean.tb_user;
import com.yc.model.JsonModel;
import com.yc.web.servlets.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-06-26 16:09
 */
@WebServlet("/admin/order.action")
public class TbOrderServlet extends BaseServlet {
    public void findAllTbOrder(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        tb_order order = super.parseObjectFromRequest(req, tb_order.class);
        int page = 1;
        if (req.getParameter("page") != null) {
            page = Integer.parseInt(req.getParameter("page"));
        }
        int limit = 10;
        if (req.getParameter("limit") != null) {
            limit = Integer.parseInt(req.getParameter("limit"));
        }
        String sql1 = "select*from tb_order where 1=1  ";
        String sql2 = "select count(*) as total from tb_order where 1=1 ";
        int skip = (page - 1) * limit;
        List<Object> params = new ArrayList<>();
        if (order.getId() != null) {
            sql1 += " and id=? ";
            sql2 += " and id=? ";
            params.add(order.getId());
        }
        if (order.getOrder_code() != null) {
            sql1 += " and order_code=? ";
            sql2 += " and order_code=? ";
            params.add(order.getOrder_code());
        }
        if (order.getTable_id()!=null){
            sql1+=" and table_id=? ";
            sql2+=" and table_id=? ";
            params.add(order.getTable_id());
        }
        List<tb_order> list = dbHelper.select(tb_order.class, sql1 + " order by CASE WHEN disabled = 0 THEN 0 ELSE 1 END,order_date DESC  limit " + limit + " offset " + skip, params.toArray());
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> result = dbHelper.select(sql2, params.toArray());
        int total = Integer.parseInt(result.get(0).get("total").toString());
        map.put("code", 0);
        map.put("msg", "查询用户成功");
        map.put("count", total);
        map.put("data", list);
        super.writeObjectToJson(map, resp);
    }
    public void deleteAll(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("idsStr");
        String[] parts = id.split(",");
        String sql = "update tb_order set disabled=? where id=?";
        for (String str : parts) {
            dbHelper.doUpdate(sql,1,str);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "删除成功");
        super.writeObjectToJson(map, resp);
    }
    public void delete(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String sql="update tb_order set disabled=? where id=?";
        dbHelper.doUpdate(sql,1,id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "删除成功");
        super.writeObjectToJson(map, resp);
    }
    public void updateOrder(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        tb_order order=super.parseObjectFromRequest(req,tb_order.class);
        String sql="update tb_order set order_code=?,table_id=?,order_status=?,disabled=?,update_date=? where id=?";
        int result=dbHelper.doUpdate(sql,order.getOrder_code(),order.getTable_id(),order.getOrder_status(),order.getDisabled(), LocalDateTime.now(),order.getId());
        Map<String,Object>map=new HashMap<>();
        if (result > 0) {
            map.put("code", 0);
            map.put("msg", "更新成功");
        } else {
            map.put("code", 1);
            map.put("msg", "更新失败");
        }
        super.writeObjectToJson(map, resp);
    }
}
